<template>
  <!-- 组织架构表单 -->
  <el-dialog :visible.sync="localVisible" :title="title" width="500px" @close="handleClose">
    <el-form label-width="120px" :model="form" :rules="rules" ref="addUser">
      <el-form-item prop="user_name" label="用户名">
        <el-input v-model="form.user_name" size="mini" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item prop="nick_name" label="名称">
        <el-input v-model="form.nick_name" size="mini" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item prop="avatar" label="头像">
        <image-upload v-model="form.avatar" />
      </el-form-item>
      <el-form-item prop="phone" label="手机号">
        <el-input v-model="form.phone" size="mini" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="form.email" size="mini" style="width: 300px"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
        <el-button size="mini" @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import ImageUpload from '@/components/ImageUpload'
import { saveUser, getUserDetail } from '@/api/user';
export default {
  name: 'organizationAdd',
  components: {
    ImageUpload
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
      required: true
    },
    title: {
      type: String,
      default: '新增角色'
    },
    userId: {
      type: Number,
      default: null,
    }
  },
  data() {
    return {
      localVisible: this.showDialog,
      form: {
        user_name: '',
        nick_name: '',
        phone: '',
        email: '',
        avatar: ''
      },
      rules: {
        user_name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
        ],
        nick_name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
        ],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
          //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱格式',
            trigger: ['blur', 'change']
          }
        ]
      }
    };
  },
  watch: {
    // 监听父组件传递的 visible，同步到本地变量
    showDialog(newVal) {
      this.localVisible = newVal;
      this.resetForm(); // 先重置表单
      if (this.userId != null) {
        this.getuserDetail();
      }
    },
    // 监听本地变量的变化，通知父组件更新 visible
    localVisible(newVal) {
      if (newVal !== this.showDialog) {
        this.$emit('update:showDialog', newVal);
      }
    }
  },
  methods: {
    btnOk() {
      this.$refs.addUser.validate(async isOk => {
        if (isOk) {
          let msg = '新增'

          if (this.userId) {
            msg = '编辑'
            console.log('编辑');

            await saveUser({ ...this.form, id: this.userId })
          } else {
            //调用接口
            await saveUser(this.form)
          }

          // 通知父组件更新
          this.$emit('submit')
          //成功提示
          this.$message.success(`${msg}用户成功`)
          //重置表单
          this.handleClose()
        }
      })
    },
    handleClose() {
      this.localVisible = false; // 修改本地变量
      this.resetForm(); // 清理表单数据
    },
    resetForm() {
      if (this.$refs.addUser) {
        this.$refs.addUser.resetFields(); // 重置表单
        this.form = {
          user_name: '',
          nick_name: '',
          phone: '',
          email: '',
          avatar: ''
        }
      }
    },
    //获取用户详情
    async getuserDetail() {
      this.form = await getUserDetail({ id: this.userId })
    }


  }
};
</script>

<style scoped>
/* 可以在这里添加样式 */
</style>